﻿<div class="body_page">
    <div id="Roles_TableContainer"></div>
</div>

<div style="padding:5px 0; text-align:center; position:relative; top:-5px">
        <a onclick="return _openWindowAddRoles();" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'">Thêm ROLE</a>
</div>

<script type="text/javascript">

    var IndexMenuOnRolesWorking = -1;

    $(document).ready(function () {

        $('#Roles_TableContainer').datagrid({
            title: 'Danh Sách ROLES',
            width: $(window).width() - 30,
            height: $(window).height() - 160,
            fitColumns: true,
            scrollbarSize: 0,
            idField: 'ROLEID',
            singleSelect: true,
            url: _nameSubDomain + "/PhanQuyen/RolesList",
            columns: [[
                { field: 'ROLEID', resizable: false, hidden: true },
                { field: 'TEN', resizable: false, title: 'Tên', sortable: true, width: getwidthcolum(60) },
                { field: 'GHICHU', resizable: false, title: 'Ghi chú', sortable: true, width: getwidthcolum(20) },
                {
                    field: 'action',
                    title: '',
                    width: getwidthcolum(10),
                    align: 'center',
                    formatter: function (value, row, index) {
                        return '<button title="Edit Record" onclick="return _openWinDownEditRoles(' + row.ROLEID + ');" class="jeasyui-command-button jeasyui-edit-command-button"></button>&nbsp;<button title="Delete" onclick="return _deleteROLES(' + row.ROLEID + ',' + index + ');" class="jeasyui-command-button jeasyui-delete-command-button"></button>';
                    }
                }
            ]],
            view: detailview,
            detailFormatter: function (index, row) {
                return '<div style="padding:15px 10px; position: relative;">'
                       + '<a onclick="return OnpenWinAddMenuOnRoles(' + row.ROLEID + ',' + index + ')" href="#" class="easyui-linkbutton l-btn l-btn-small button_on_child_menu" group="" id=""><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">Thêm menu trên role ' + row.TEN + '</span><span class="l-btn-icon icon-add">&nbsp;</span></span></a>'
                       + '<table class="dd_menu_on_roles"></table></div>';
            },
            onExpandRow: function (index, row) {
                var IndexRowRoleParent = index;
                var ddv = $(this).datagrid('getRowDetail', index).find('table.dd_menu_on_roles');
                ddv.datagrid({
                    title: 'Danh Sách Menu trên role ' + row.TEN,
                    url: _nameSubDomain + '/PhanQuyen/RoleMenuList?rolesId=' + row.ROLEID,
                    fitColumns: true,
                    singleSelect: true,
                    //rownumbers:true,
                    //loadMsg: '',
                    width: $(window).width() - 90,
                    idField: 'RMID',
                    columns: [[
                        {
                            field: 'ROLEID', title: 'Roles', resizable: false, width: getwidthcolum(20), align: 'left', formatter: function (value, row, index) {
                                if (value != null) {
                                    return Get_Roles_By_Id(value).TEN;
                                }
                                else {
                                    return "";
                                }
                            }
                        },
                        {
                            field: 'MENUID', title: 'Menu', resizable: false, width: getwidthcolum(20), align: 'left', formatter: function (value, row, index) {
                                if (value != null) {
                                    var datareturn = Get_Menu_By_Id(value);
                                    if (datareturn == null) return "";
                                    else return datareturn.TEN;
                                }
                                else {
                                    return "";
                                }
                            }
                        },
                        {
                            field: 'action',
                            title: '',
                            width: getwidthcolum(10),
                            align: 'center',
                            formatter: function (value, row, index) {
                                return '<button title="Edit Record" onclick="return _openWinDownEditMenuOnRoles(' + row.RMID + ',' + IndexRowRoleParent + ');" class="jeasyui-command-button jeasyui-edit-command-button"></button>&nbsp;<button onclick="return _deleteRolesOnMenu(' + row.RMID + ',' + IndexRowRoleParent + ',' + index + ');" title="Delete" class="jeasyui-command-button jeasyui-delete-command-button"></button>';
                            }
                        }

                    ]],
                    onResize: function () {
                        $('#Roles_TableContainer').datagrid('fixDetailRowHeight', index);
                    },
                    onLoadSuccess: function () {
                        setTimeout(function () {
                            $('#Roles_TableContainer').datagrid('fixDetailRowHeight', index);
                        }, 0);
                    },
                    onDblClickRow: function (index, row) {
                        IndexMenuOnRolesWorking = index;
                        loadwindowOnContent(_nameSubDomain + "/PhanQuyen/EditMenuOnRoles?RID=" + row.RMID, "window_Edit_Menu_Roles", "Cập nhật thông tin menu trên role", 330, 210);
                    }
                });
                $('#Roles_TableContainer').datagrid('fixDetailRowHeight', index);
            },
            onLoadSuccess: function () {
                if (IndexMenuOnRolesWorking != -1) {
                    $('#Roles_TableContainer').datagrid('expandRow', IndexMenuOnRolesWorking);
                }
            },
            onDblClickRow: function (index, row) {
                IndexMenuOnRolesWorking = index;
                loadwindowOnContent(_nameSubDomain + "/PhanQuyen/UpdateRoles?rolesId=" + row.ROLEID, "window_Edit_ROLES", "Cập thông tin ROLES", 230, 210);
            }
        });
    });

    function _openWinDownEditRoles(RID) {
        loadwindowOnContent(_nameSubDomain + "/PhanQuyen/UpdateRoles?rolesId=" + RID, "window_Edit_ROLES", "Cập thông tin ROLES", 350, 150);
    }

    function _openWindowAddRoles() {
        loadwindowOnContent(_nameSubDomain + "/PhanQuyen/AddUpdateRoles", "window_Edit_ROLES", "Thêm thông tin ROLES", 350, 150);
    }
    
    function _deleteROLES(ROLEID, Index) {

        var rows = $('#Roles_TableContainer').datagrid('getRows');    // get current page rows
        var row = rows[Index];

        $.confirm({
            text: "Bạn chắc muốn xóa ROLES " + row.TEN + " !. Chú ý : tất cả các thông tin liên quan cũng sẽ được xóa",
            confirm: function (button) {

                $('#Roles_TableContainer').mask('ROLES ' + row.TEN + 'đang được xóa khởi hệ thống');

                $.get(_nameSubDomain + '/PhanQuyen/DestroyRoles?rolesid=' + ROLEID, function (data) {

                    if (data == "ERROR") {
                        confirmOptionNormal("Có lỗi xảy ra, vui lòng nhập F5 hoặc tải lại trang web");
                    }
                    else {
                        $('#Roles_TableContainer').datagrid('reload');
                    }


                    $('#Roles_TableContainer').unmask();
                });

            },
            cancel: function (button) {

            }
        });

        return false;
    } 

    function OnpenWinAddMenuOnRoles(rolesId, Index) {

        IndexMenuOnRolesWorking = Index;
        loadwindowOnContent(_nameSubDomain + "/PhanQuyen/AddMenuOnRoles?RID=" + rolesId, "window_Edit_Menu_Roles", "Thêm thông tin menu trên role", 330, 210);

    }

    function _openWinDownEditMenuOnRoles(MRID, Index) {

        IndexMenuOnRolesWorking = Index;
        loadwindowOnContent(_nameSubDomain + "/PhanQuyen/EditMenuOnRoles?RID=" + MRID, "window_Edit_Menu_Roles", "Cập nhật thông tin menu trên role", 330, 210);
    }

    function _deleteRolesOnMenu(MID, Index, IndexChild) {

        _deleteRolesOnMenu = Index;

        var ddv_Parent = $("#Roles_TableContainer").datagrid('getRowDetail', Index).find('table.dd_menu_on_roles');

        var rows = ddv_Parent.datagrid('getRows');    // get current page rows
        var row = rows[IndexChild];



        $.confirm({
            text: "Bạn chắc muốn xóa menu " + Get_Menu_By_Id (row.MENUID).TEN + " on roles: " + Get_Roles_By_Id(row.ROLEID).TEN + "!",
            confirm: function (button) {

                $('#Roles_TableContainer').mask('Menu on roles ' + row.TEN + 'đang được xóa khởi hệ thống');

                $.get(_nameSubDomain + '/PhanQuyen/DestroyRolesOnMenu?RMID=' + MID, function (data) {

                    $('#Roles_TableContainer').datagrid('reload');
                    $('#Roles_TableContainer').unmask();
                });

            },
            cancel: function (button) {

            }
        });

        return false;

    }


</script>
